package com.epoch.zuul.auth.service.impl;

import bean.response.ResponseBean;
import bean.response.ResultEnum;
import com.epoch.zuul.auth.dao.AuthRoleDao;
import com.epoch.zuul.auth.dao.RolesDao;
import com.epoch.zuul.auth.role.Auth;
import com.epoch.zuul.auth.role.Roles;
import com.epoch.zuul.auth.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.ResultInfoUtil;

import java.util.List;

/**
 * @author admin
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RolesDao rolesDao;

    @Autowired
    private AuthRoleDao authRoleDao;

    @Override
    public Roles getRoleById(Integer roleId) {
        return rolesDao.getRoleById(roleId);
    }

    @Override
    public ResponseBean addRole(Roles role) {
        //对创建该角色的账户进行验证，验证成功 创建角色
        rolesDao.addRole(role);
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"创建角色成功",rolesDao.getRoleByRoleName(role.getRoleName()));
    }

    @Override
    public ResponseBean deleteRole(String roleName) {
        //对该账户进行验证 验证成功 删除角色
        if (rolesDao.deleteRole(roleName) < 1){
            return ResultInfoUtil.buildError(ResultEnum.INVALID_REQUEST,"删除角色失败");
        }
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"删除角色成功",null);
    }

    @Override
    public ResponseBean updateRole(Roles roles) {
        //对该账户进行验证 验证成功 修改该角色的相关信息
        rolesDao.updateRole(roles);
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"修改角色的相关信息成功",rolesDao.getRoleByRoleName(roles.getRoleName()));
    }

    @Override
    public ResponseBean getRoleList() {
        List<Roles> rolesList =  rolesDao.getRoleList();
        //将每个角色的权限列表加入
        for (int i = 0; i < rolesList.size(); i++){
            //获取该角色的权限列表 通过roleId 并加入
            //获取一级权限
            Integer roleId = rolesList.get(i).getRoleId();
            List<Auth> authList = authRoleDao.getFirstAuths(roleId);
            //做处理 处理为树形结构
            //对该一级权限获取所有的二级权限
            for (int k = 0; k < authList.size(); k++) {

                //填写该一级权限的所有二级权限
                Integer pidFirst = authList.get(k).getAuthId();
                List<Auth> second = authRoleDao.getAuthByPid(pidFirst, roleId);
                //填写该二级权限
                for (int j = 0; j < second.size(); j++) {
                    //获取该pid对应的子权限
                    Integer pidSecond = second.get(j).getAuthId();
                    List<Auth> third = authRoleDao.getAuthByPid(pidSecond, roleId);
                    //填写三级权限
                    second.get(j).setAuths(third);
                }
                authList.get(k).setAuths(second);
            }
            rolesList.get(i).setAuthList(authList);
        }
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"获取角色列表成功", rolesList);
    }

    @Override
    public ResponseBean getRoleByRoleName(String roleName) {
        return ResultInfoUtil.buildSuccess(ResultEnum.OK, "获取角色信息成功", rolesDao.getRoleByRoleName(roleName));
    }
}
